let defaultAvatar = 'https://qncdn.mopic.mozigu.net/f/mr6kod18k32ah1s63tg/1263ee2f88f9/syshead.png';

Component({
    properties: {},
    data: {
        logoUrl: 'https://qncdn.mopic.mozigu.net/f/mr6kod18k32ah1s63tg/126418ec86ac/icon.jpeg',
        avatarUrl: defaultAvatar,
        isNewest: null,
        inputValue: '',
        disabledStatus: false,
        keyCode: null
    },
    methods: {
        clickAvatarNew() {
            getApp().mox.stat(`click_auth_avatar`);
        },
        clickAvatarOld() {
            getApp().mox.stat(`click_auth_avatar`);
        },
        onChooseAvatar(evt) {
            this.setData({
                disabledStatus: true
            });
            wx.uploadFile({
                url: 'https://api.scf.moapp.net.cn/release/upload_cos?sync_verify=1',
                filePath: evt.detail?.avatarUrl,
                name: 'file',
                formData: {
                    path: 'weapp/wx5b5cadb2d78b45d5'
                },
                success: (res) => {
                    const pkg = JSON.parse(res.data);
                    this.setData({
                        avatarUrl: pkg.code == 'SUCCESS' ? pkg.data.url : this.data.avatarUrl,
                        disabledStatus: false
                    });
                    getApp().mox.stat(`auth_avatar_success`);
                },
                fail: (err) => {
                    getApp().mox.stat(
                        `auth_avatar_fail: ${err.errMsg || err.msg || '未知错误'}`
                    );
                }
            });
        },
        chooseImage() {
            this.setData({
                disabledStatus: true
            });
            wx.chooseMedia({
                count: 1,
                mediaType: ['image'],
                sourceType: ['album', 'camera'],
                camera: 'back',
                success: (res) => {
                    wx.uploadFile({
                        url: 'https://api.scf.moapp.net.cn/release/upload_cos?sync_verify=1',
                        filePath: res.tempFiles[0].tempFilePath,
                        name: 'file',
                        formData: {
                            path: 'weapp/wx5b5cadb2d78b45d5'
                        },
                        success: (res) => {
                            const pkg = JSON.parse(res.data);
                            this.setData({
                                avatarUrl:
                                    pkg.code == 'SUCCESS' ? pkg.data.url : this.data.avatarUrl,
                                disabledStatus: false
                            });
                            getApp().mox.stat(`auth_avatar_success`);
                        },
                        fail: (err) => {
                            getApp().mox.stat(
                                `auth_avatar_fail: ${
                                    err.errMsg || err.msg || '未知错误'
                                }`
                            );
                        }
                    });
                    getApp().mox.stat(`choose_avatar_success`);
                },
                fail: (err) => {
                    getApp().mox.stat(
                        `choose_avatar_fail: ${err.errMsg || err.msg || '未知错误'}`
                    );
                }
            });
        },
        onBlur() {
            getApp().mox.stat(`cancle_auth_nickname`);
        },
        clickNickname() {
            getApp().mox.stat(`click_auth_nickname`);
        },
        getInputValue(evt) {
            this.setData({
                inputValue: evt.detail.value,
                keyCode: evt.detail?.keyCode ? 'input' : 'auth'
            });
            getApp().mox.stat(`modify_nickname`);
        },
        deny() {
            getApp().mox.stat(`user_deny_auth`);
            this.triggerEvent('closeUserInfoDialog');
        },
        confirm() {
            let temp = {
                nickName: !this.data.inputValue ? '我' : this.data.inputValue,
                avatarUrl: this.data.avatarUrl
            };
            if (this.data.inputValue == '我' && this.data.avatarUrl == defaultAvatar) {
                temp.isVirtualUser = true;
            }
            wx.setStorage({
                key: '__session__',
                data: JSON.stringify({
                    ...getApp().mox.user,
                    ...temp
                }),
                success: () => {
                    this.triggerEvent('confirmUserInfoDialog');
                }
            });
            getApp().mox.stat(`user_confirm_auth`);
        },
        setDefaultUserInfo() {
            let userInfo = wx.getStorageSync('__session__');
            let parseUserInfo = userInfo ? JSON.parse(userInfo) : {};
            this.setData({
                avatarUrl: parseUserInfo?.isVirtualUser
                    ? defaultAvatar
                    : parseUserInfo?.avatarUrl || defaultAvatar,
                inputValue: parseUserInfo?.isVirtualUser ? '' : parseUserInfo?.nickName
            });
        },
        touchMove() {},
        compareVersion(v1, v2) {
            v1 = v1.split('.');
            v2 = v2.split('.');
            const len = Math.max(v1.length, v2.length);

            while (v1.length < len) {
                v1.push('0');
            }
            while (v2.length < len) {
                v2.push('0');
            }

            for (let i = 0; i < len; i++) {
                const num1 = parseInt(v1[i]);
                const num2 = parseInt(v2[i]);

                if (num1 > num2) {
                    return 1;
                } else if (num1 < num2) {
                    return -1;
                }
            }

            return 0;
        }
    },
    created: function () {},
    attached: function () {
        this.setDefaultUserInfo();
        const version = wx.getSystemInfoSync().SDKVersion;
        console.log('version----------------', version, this.compareVersion(version, '2.21.2'));
        if (this.compareVersion(version, '2.21.2') >= 0) {
            this.setData({
                isNewest: true
            });
        } else {
            this.setData({
                isNewest: false
            });
        }
        getApp().mox.stat(`pullUp_userInfo_dialog`);
    },
    ready: function () {},
    moved: function () {},
    detached() {
        if (this.data.keyCode == 'input') {
            getApp().mox.stat(`user_input_nickname`);
        } else if (this.data.keyCode == 'auth') {
            getApp().mox.stat(`auth_user_nickname_success`);
        }
    }
});
